package org.itboys.mobile.service.mongo.mobile;

import org.itboys.kitchen.entity.mongo.KitchenExpressMode;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;


/**
 * 作者：jiangxiong
 * 日期：2016年11月25日
 * 联系方式 ：empty90911@126.com
 * 描述：农厨-运费-service-mobil
 */
@Service
public class KitchenMobileExpressModeService extends BaseMongoService<KitchenExpressMode> {
	
	
	private static final long serialVersionUID = 2415524731052541775L;
	@Resource(name="kitchenDs")
	private MongoDataSource haoyiDataSource;
	@Override
	protected MongoDataSource getMongoDataSource() {
		return haoyiDataSource;
	}

	@Override
	protected Class<KitchenExpressMode> getEntityClass() {
		return KitchenExpressMode.class;
	}

	/**
	 * 匹配运费
	 * @param payAmount
	 * @return
	 */
	public Map<String,Object> matching(Double payAmount){
		Map<String,Object> result = new HashMap<String,Object>();
		Double sendAmount = 0.00;
		StringBuilder msg= new StringBuilder();
		//查找运费模板
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("isDeleted",0);
		KitchenExpressMode expressMode=super.list(param).get(0);
		Double baseAmount1 = expressMode.getBaseAmount1();
		Double baseAmount2 = expressMode.getBaseAmount2();
		Double baseAmount3 = expressMode.getBaseAmount3();
		Double expressAmount1 = expressMode.getExpressAmount1();
		Double expressAmount2 = expressMode.getExpressAmount2();
		Double expressAmount3 = expressMode.getExpressAmount3();
		if(payAmount>0&&payAmount<baseAmount1){
			sendAmount=expressAmount1;

		}else if(payAmount>=baseAmount1&&payAmount<baseAmount2){
			sendAmount=expressAmount2;

		}else if(payAmount>=baseAmount2&&payAmount<baseAmount3){
			sendAmount=expressAmount3;

		}else{
			sendAmount=0.00;
		}
		result.put("sendAmount", new BigDecimal(sendAmount).setScale(2,BigDecimal.ROUND_HALF_UP));
		if(sendAmount>0){
			msg.append("配送费："+new BigDecimal(sendAmount).setScale(2,BigDecimal.ROUND_HALF_UP)+"元，满"+baseAmount3+"元免配送费。");
		}else{
			msg.append("满"+new BigDecimal(baseAmount3).setScale(2,BigDecimal.ROUND_HALF_UP)+"元免配送费。");
		}
		/*msg.append("1，购物金额不满："+baseAmount1+"元，配送费："+expressAmount1+"元。");
		msg.append("\n");
		msg.append("2，购物金额满："+baseAmount1+"元，不足"+baseAmount2+"元，配送费"+expressAmount2+"元。");
		msg.append("\n");
		msg.append("3，购物金额满："+baseAmount2+"元，不足"+baseAmount3+"元，配送费"+expressAmount3+"元。");
		msg.append("\n");
		msg.append("4，购物金额超过："+baseAmount3+"元，免配送费。");*/

		result.put("sendMsg",msg.toString());
		return result;
	}

}
